package artid.com.arttogoa.util;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager.OnActivityResultListener;

import artid.com.arttogoa.app.ArtWorkApp;
import artid.com.arttogoa.db.DataKeeper;
import artid.com.arttogoa.parser.UserItem;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners.SnsPostListener;
import com.umeng.socialize.controller.listener.SocializeListeners.UMShareBoardListener;
import com.umeng.socialize.media.QQShareContent;
import com.umeng.socialize.media.QZoneShareContent;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.QZoneSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.utils.Log;
import com.umeng.socialize.weixin.controller.UMWXHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.umeng.socialize.weixin.media.WeiXinShareContent;

public class AppUtil {
	public static boolean isTest = false;
	private static UserItem user;
	public static String picWidthUrl = "_" + "400" + "x10.png";

	// public static UMSocialService mController;

	public static void Intent(Context packageContext, Class<?> cls) {
		Intent intent = new Intent(packageContext, cls);
		packageContext.startActivity(intent);
	}

	public static void Intent(Context packageContext, Class<?> cls,
			Bundle bundle) {

		Intent intent = new Intent(packageContext, cls);
		intent.putExtras(bundle);
		packageContext.startActivity(intent);

	}

	public static void setUser() {
		AppUtil.user = (UserItem) DataKeeper.readSerializable(
				ArtWorkApp.getContext(), UserItem.class);
		if (AppUtil.user == null) {
			AppUtil.user = new UserItem();
		}
	}

	public static UserItem getUser() {
		if (user == null) {
			setUser();
		}
		return user;
	}

	// public static View getInputItem(LayoutInflater inflater) {
	// return inflater.inflate(R.layout.item_input, null, false);
	// }
	//
	// public static View getInputItemIncludingBtn(LayoutInflater inflater) {
	// return inflater.inflate(R.layout.item_input_includebtn, null, false);
	// }
	//
	// public static View gettTexItem(LayoutInflater inflater) {
	// return inflater.inflate(R.layout.item_text, null, false);
	// }
	//
	// public static View getSpinnerItem(LayoutInflater inflater) {
	// return inflater.inflate(R.layout.item_spinner, null, false);
	// }

	/****** 监听器 start *******/
	private static List<OnActivityResultListener> list;

	public static void addOnActivatyResultListener(OnActivityResultListener l) {
		if (null == list) {
			list = new ArrayList<OnActivityResultListener>();
		}
		if (l != null && !list.contains(l)) {
			list.add(l);
		}
	}

	public static void onActivityResult(int requestCode, int resultCode,
			Intent data) {
		if (list == null) {
			return;
		}
		for (OnActivityResultListener l : list) {
			l.onActivityResult(requestCode, resultCode, data);
		}
	}

	// static int sharePicId = R.drawable.logo2;
	public static UMSocialService mController;
	public static void showShare(final Context context, String title1,
			String shareContent, String imageUrl, final String target_url) {
		Log.e("imageurl", imageUrl);
		Log.e("target_url", target_url);
		// ShareDialog dialog = new ShareDialog(this);
		// dialog.show();

		String title = title1;
		mController = UMServiceFactory.getUMSocialService("com.umeng.share");
		SnsPostListener mSnsPostListener = new SnsPostListener() {

			@Override
			public void onStart() {
				// Toast.makeText(context,
				// "分享开始", Toast.LENGTH_SHORT)
				// .show();
			}
			@Override
			public void onComplete(SHARE_MEDIA platform, int stCode,
					SocializeEntity entity) {
				if (stCode == 200) {
					// requestReduceMoney();

				} else {
					// Toast.makeText(context, "分享失败",
					// Toast.LENGTH_SHORT).show();
				}
			}
		};
		mController.registerListener(mSnsPostListener);
		// 设置分享内容
		if(shareContent.length()>20){
			shareContent = shareContent.substring(0,18)+"...";
		}
		mController.setShareContent(shareContent);
		mController.setShareMedia(new UMImage(context, imageUrl));
		mController.setShareImage((new UMImage(context, imageUrl)));
		//		CustomPlatform customPlatform = null;


		//			customPlatform = new CustomPlatform("复制链接", "复制链接",
		//					R.drawable.copy);
		//			customPlatform.mClickListener = new OnSnsPlatformClickListener() {
		//				@SuppressLint("NewApi")
		//				@Override
		//				public void onClick(Context context, SocializeEntity entity,
		//						SnsPostListener listener) {
		//					Toast.makeText(context, "复制链接成功", Toast.LENGTH_SHORT).show();
		//					SocializeUtils.sendAnalytic(context,
		//							"com.umeng.custopm.copy_link", "分享内容", null,
		//							"copy_link");
		//					ClipboardManager myClipboard;
		//					myClipboard = (ClipboardManager) context
		//							.getSystemService(Service.CLIPBOARD_SERVICE);
		//					ClipData cd = ClipData.newPlainText("label", target_url);
		//					myClipboard.setPrimaryClip(cd);
		//				}
		//			};


		//
		// mController.getConfig().setPlatforms(SHARE_MEDIA.WEIXIN,
		// SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.QZONE);
		// mController.setShareMedia();
		//		mController.setShareImage((new UMImage(context, R.drawable.logo2)));
		//		mController.getConfig().addCustomPlatform(customPlatform);
		mController.getConfig().removePlatform(SHARE_MEDIA.SINA);
		mController.getConfig().removePlatform(SHARE_MEDIA.TENCENT);
		// // QQ分享
		UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler((Activity) context,
				Constant.TENCENT_APP_KEY, Constant.TENCENT_APP_SECRET);
		qqSsoHandler.addToSocialSDK();
		QQShareContent qqShareContent = new QQShareContent();
		qqShareContent.setTitle(title);
		qqShareContent.setShareContent(shareContent);
		qqShareContent.setShareImage(new UMImage(context, imageUrl));
		qqShareContent.setTargetUrl(target_url);
		mController.setShareMedia(qqShareContent);
		// QQ空间分享
		QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(
				(Activity) context, Constant.QZONE_APP_ID,
				Constant.QZONE_APP_KEY);
		qZoneSsoHandler.addToSocialSDK();
		QZoneShareContent qZoneShareContent = new QZoneShareContent();
		qZoneShareContent.setTitle(title);
		qZoneShareContent.setShareContent(shareContent);
		qZoneShareContent.setShareImage(new UMImage(context, imageUrl));
		qZoneShareContent.setTargetUrl(target_url);
		mController.setShareMedia(qZoneShareContent);

		// 添加微信平台
		UMWXHandler wxHandler = new UMWXHandler(context, Constant.WX_APP_ID,
				Constant.WX_APP_KEY);
		wxHandler.addToSocialSDK();

		UMWXHandler wxCircleHandler = new UMWXHandler(context,
				Constant.WX_APP_ID, Constant.WX_APP_KEY);
		wxCircleHandler.setToCircle(true);
		wxCircleHandler.addToSocialSDK();

		WeiXinShareContent weixinContent = new WeiXinShareContent();
		weixinContent.setShareContent(shareContent);
		weixinContent.setTitle(title);
		weixinContent.setTargetUrl(target_url);
		weixinContent.setShareMedia(new UMImage(context, imageUrl));
		mController.setShareMedia(weixinContent);

		// 设置朋友圈分享的内容
		CircleShareContent circleMedia = new CircleShareContent();
		circleMedia.setShareContent(shareContent);
		circleMedia.setTitle(title + "--" + shareContent);
		circleMedia.setTargetUrl(target_url);
		circleMedia.setShareImage(new UMImage(context, imageUrl));
		mController.setShareMedia(circleMedia);
		mController.getConfig().closeToast();
		mController.setShareBoardListener(new UMShareBoardListener() {

			@Override
			public void onShow() {
				// TODO Auto-generated method stub

			}

			@Override
			public void onDismiss() {
				// TODO Auto-generated method stub
				// Log.e("!!!class",context.getClass().toString());
				if (context.getClass().toString().contains("MyRedActivity")) {
					return;
				}

			}
		});

		mController.openShare((Activity) context, mSnsPostListener);

	}


}
